<?php

/**
 * This is the model class for table "qshift".
 *
 * The followings are the available columns in table 'qshift':
 * @property integer $idqshift
 * @property string $name
 * @property string $description
 * @property string $start_time
 * @property string $end_time
 * @property string $enabled
 * @property string $availability
 * @property string $swap_allowed
 * @property integer $team_id
 *
 * The followings are the available model relations:
 * @property Callback[] $callbacks
 * @property Team $team
 * @property QshiftChange[] $qshiftChanges
 * @property QshiftChange[] $qshiftChanges1
 * @property Schedule[] $schedules
 */
class Qshift extends CActiveRecord
{

	public static $yesNoValues= array('0'=>'No', '1'=>'Yes');

	 public static function getYesNoValue($bin=null){
	 
		if($bin!==null)
			return self::$yesNoValues[$bin];
		return self::$yesNoValues;
	 }

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'qshift';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, start_time, end_time, team_id', 'required'),
			array('team_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>10),
			array('description', 'length', 'max'=>45),
			array('enabled, availability, swap_allowed', 'length', 'max'=>1),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idqshift, name, description, start_time, end_time, enabled, availability, swap_allowed, team_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'callbacks' => array(self::HAS_MANY, 'Callback', 'idqshift'),
			'team_relation' => array(self::BELONGS_TO, 'Team', 'team_id'),
			'qshiftChanges' => array(self::HAS_MANY, 'QshiftChange', 'current_qshift'),
			'qshiftChanges1' => array(self::HAS_MANY, 'QshiftChange', 'target_qshift'),
			'schedules' => array(self::HAS_MANY, 'Schedule', 'idqshift'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idqshift' => 'Id',
			'name' => 'QShift Name',
			'description' => 'Description',
			'start_time' => 'Start Time',
			'end_time' => 'End Time',
			'enabled' => 'Enabled',
			'availability' => 'Availability',
			'swap_allowed' => 'Swaps Allowed',
			'team_id' => 'Team',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($team_id=null)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		if($team_id != null)
			$criteria->condition = 'team_id = '.$team_id;		
		
		$criteria->compare('idqshift',$this->idqshift);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('start_time',$this->start_time,true);
		$criteria->compare('end_time',$this->end_time,true);
		$criteria->compare('enabled',$this->enabled,true);
		$criteria->compare('availability',$this->availability,true);
		$criteria->compare('swap_allowed',$this->swap_allowed,true);
		$criteria->compare('team_id',$this->team_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Qshift the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
